package cn.wolfcode.luowowo.common.util;

import cn.wolfcode.luowowo.common.exception.LogicException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 参数判断工具类
 */
public class AssertUtil {
    /**
     * 判断传入参数是否有长度
     *
     * @param value
     * @param msg   异常信息
     */
    public static void hasLength(String value, String msg) {
        if (value == null || "".equals(value.trim())) {
            throw new LogicException(msg);
        }
    }

    /**
     * @param v1  密码
     * @param v2  确认密码
     * @param msg 判断密码一致不
     */
    public static void hasEquals(String v1, String v2, String msg) {
        if (v1 == null || v2 == null) {
            throw new LogicException("传入的参数不允许为null");
        }
        //先在前面判断是不是空才能这样判断
        if (!v1.equals(v2)) {
            throw new LogicException(msg);
        }
    }

    public static void checkPhone(String phone) {
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        if (phone.length() != 11) {
           throw new LogicException("手机号为11位数");
        } else {
            Pattern compile = Pattern.compile(regex);
            Matcher matcher = compile.matcher(phone);
            boolean flag = matcher.matches();
            if (!flag) {
                throw new LogicException("手机号格式不正确");
            }
        }
    }
}
